package com.pg_chat.utils;

import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.StandardCharsets;


public class MD5Util {
    public static String encode(String sources){
        return encode(sources.getBytes(StandardCharsets.UTF_8));
    }

    public static String encode(byte[] sources){
        String dest = "";
        MD5Digest md5Digest = new MD5Digest();
        //加密源数据加载到md5模型中
        md5Digest.update(sources,0,sources.length);
        byte[] hash = new byte[md5Digest.getDigestSize()];
        //进行数据加密，将结果赋值给hash
        md5Digest.doFinal(hash,0);
        dest = Hex.toHexString(hash);
        return dest;
    }

    public static void main(String[] args){
//        String sources =  "\"undefined\"";
        String sources = "eyJwaG9uZSI6IjEzNzUwMTc4ODk4IiwicXJjb2RlaWQiOiIxIn0%3D1638329313DrVlanVisitor";
        String correct = "eabbadf71fd910dc961a47e3dd1df5c0";
        String dest = encode(sources);
        System.out.println("dest: "+dest+"\ncorrect: "+correct);
        System.out.println(correct.equals(dest));
    }
}
